package com.itheima.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.CatalogMapper;
import com.itheima.dao.store.CourseMapper;
import com.itheima.dao.store.QuestionMapper;
import com.itheima.domain.store.Catalog;
import com.itheima.domain.store.Course;
import com.itheima.domain.store.Question;
import com.itheima.service.store.CatalogService;
import com.itheima.utils.MysqlUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

/**
 * CatalogServiceImpl
 * 2021/10/17 0:07
 * created by yangyawen
 */
public class CatalogServiceImpl implements CatalogService {
    // 分页查询全部
    @Override
    public PageInfo findByPage(String pageNum) {
        int page = pageNum==null||pageNum=="" ? 1 : Integer.parseInt(pageNum);
        PageHelper.startPage(page,5);
        SqlSession sqlSession = MysqlUtils.getSqlSession();
        CatalogMapper catalogMapper = sqlSession.getMapper(CatalogMapper.class);
        List<Catalog> catalogList = catalogMapper.findAll();
        PageInfo pageInfo=new PageInfo(catalogList);
        sqlSession.close();
        return pageInfo;
    }

    // 查询全部
    @Override
    public List<Catalog> findAll() {
        SqlSession sqlSession = MysqlUtils.getSqlSession();
        CatalogMapper catalogMapper = sqlSession.getMapper(CatalogMapper.class);
        List<Catalog> catalogList = catalogMapper.findAll();
        sqlSession.close();
        return catalogList;
    }


    // 添加Catalog
    @Override
    public void addCatalog(Catalog catalog) {
        SqlSession sqlSession = MysqlUtils.getSqlSession();
        CatalogMapper catalogMapper = sqlSession.getMapper(CatalogMapper.class);
        catalog.setId(UUID.randomUUID().toString());
        catalogMapper.addCatalog(catalog);
        String courseName = catalog.getCourseName();
        sqlSession.close();
    }

    // 修改Catalog
    @Override
    public void updateCatalog(Catalog catalog) {
        SqlSession sqlSession = MysqlUtils.getSqlSession();
        CatalogMapper catalogMapper = sqlSession.getMapper(CatalogMapper.class);
        catalogMapper.updateCatalog(catalog);
        String remark = catalog.getRemark();
        sqlSession.close();

    }

    // 通过id删除单条Catalog数据
    @Override
    public void deleteCatalog(String id) {
        SqlSession sqlSession = MysqlUtils.getSqlSession();
        CatalogMapper catalogMapper = sqlSession.getMapper(CatalogMapper.class);
        catalogMapper.deleteCatalog(id);
        sqlSession.close();

    }

    //通过id查询该数据
    @Override
    public Catalog findCatalogById(String id) {
        SqlSession sqlSession = MysqlUtils.getSqlSession();
        CatalogMapper catalogMapper = sqlSession.getMapper(CatalogMapper.class);
        Catalog catalog = catalogMapper.findCatalogById(id);
        sqlSession.close();
        return catalog;
    }

    // 通过catalogId查询题目表里面与类型表关联了多少条数据
    @Override
    public List<Question> findQuestionAndCatalog(String catalogId) {
        SqlSession sqlSession = MysqlUtils.getSqlSession();
        CatalogMapper catalogMapper = sqlSession.getMapper(CatalogMapper.class);
        List<Question> questionList = catalogMapper.findQuestionAndCatalog(catalogId);
        sqlSession.close();
        return questionList;
    }


}
